﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Chuong02
{

    //Camera bóng xoay tròn xung quanh vật 
    class ArcBallCamera  :Camera
    {
        //Xoay xung quanh trục X và trục Y
        public float RotationX { get; set; }
        public float RotationY { get; set; }

        public float MinRotationY { get; set; }
        public float MaxRotationY { get; set; }

        //Khoảng cách đang đứng với Camera
        public float Distance { get; set; }

        //Giới hạn khoảng cách lại như: near và far
        public float MinDistance { get; set; }
        public float MaxDistance { get; set; }

         //Tính toán Position và Target
        public Vector3 Position { get; private set; }
        public Vector3 Target { get; set; }

        //Hàm khởi tạo
        public ArcBallCamera(Vector3 Target, float RotationX,
                            float RotationY, float MinRotationY, float MaxRotationY,
                            float Distance, float MinDistance, float MaxDistance,
                            GraphicsDevice graphicsDevice): base(graphicsDevice)
        {
            this.Target = Target;
            this.MinRotationY = MinRotationY;
            this.MaxRotationY = MaxRotationY;

            this.RotationY = MathHelper.Clamp(RotationY, MinRotationY,MaxRotationY);
            this.RotationX = RotationX;
            this.MinDistance = MinDistance;
            this.MaxDistance = MaxDistance;


            this.Distance = MathHelper.Clamp(Distance, MinDistance,MaxDistance);

        }


        //Di chuyển camera

        public void Move(float DistanceChange)
        {
            this.Distance += DistanceChange;
            this.Distance = MathHelper.Clamp(Distance, MinDistance,MaxDistance);
        }

         //Quay camera
        public void Rotate(float RotationXChange, float RotationYChange)
        {
            this.RotationX += RotationXChange;
            this.RotationY += -RotationYChange;
            this.RotationY = MathHelper.Clamp(RotationY, MinRotationY,
            MaxRotationY);
        }

        public void Translate(Vector3 PositionChange)
        {
            this.Position += PositionChange;
        }

        public override void Update()
        {
            Matrix rotation = Matrix.CreateFromYawPitchRoll(RotationX, -RotationY, 0);
            
            //Di chuyển Distance giữa camera và đối tượng
            //-->Di chuyển -->Xoay
            Vector3 translation = new Vector3(0, 0, Distance);
            translation = Vector3.Transform(translation, rotation);

            //-->Vị trí mới nè
            Position = Target + translation;

            //Tính toán ma trận up khi đã xoay
            Vector3 up = Vector3.Transform(Vector3.Up, rotation);

            View = Matrix.CreateLookAt(Position, Target, up);


            base.Update();
        }

    }
}
